<template>
    <div class="app-container">

        <el-form :inline="true" v-model="courseQuery" class="demo-form-inline">
            <el-form-item>
                <el-input placeholder="课程名称" v-model="courseQuery.title"></el-input>
            </el-form-item>

            <el-form-item>
                <el-select placeholder="课程状态" v-model="courseQuery.status">
                    <el-option label="已发布" :value="'Normal'"></el-option>
                    <el-option label="未发布" :value="'Draft'"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="添加时间" v-model="courseQuery.begin">
                <el-date-picker type="datetime" v-model="courseQuery.begin" placeholder="选择开始时间" value-format="yyy-MM-dd HH:mm:ss" default-time="00:00:00"></el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-date-picker type="datetime" v-model="courseQuery.end" placeholder="选择结束时间" value-format="yyy-MM-dd HH:mm:ss" default-time="00:00:00"></el-date-picker>
            </el-form-item>

            <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
            <el-button type="default" icon="el-icon-delete" @click="resetData()">清空</el-button>
        </el-form>


        <el-table :data="list" border fit highlight-current-row>

            <el-table-column align="center" label="序号" width="70">
                <template slot-scope="scope">
                    {{ (page-1) * limit + scope.$index + 1}}
                </template>
            </el-table-column>

            <el-table-column align="center" prop="title" label="课程名称" width="200"></el-table-column>

            <el-table-column align="center" label="课程状态" width="120">
                <template slot-scope="scope">
                    {{ scope.row.status === 'Normal' ? '已发布' : '未发布'}}
                </template>
            </el-table-column>

            <el-table-column align="center" prop="lessonNum" label="课时数"></el-table-column>
            <el-table-column align="center" prop="price" label="价格"></el-table-column>
            <el-table-column align="center" prop="gmtCreate" label="添加时间" width="200"></el-table-column>

            <el-table-column align="center" prop="viewCount" label="浏览数量" width="90"></el-table-column>

            <el-table-column label="操作" width="390" align="center">
                <template slot-scope="scope">
                    <router-link :to="'/course/info/' + scope.row.id" >
                        <el-button type="primary" size="mini" icon="el-icon-edit">编辑课程信息</el-button>
                    </router-link>
                    <router-link :to="'/course/chapter/' + scope.row.id" >
                        <el-button type="primary" size="mini" icon="el-icon-edit">编辑课程大纲</el-button>
                    </router-link>
                    <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.id)">删除课程</el-button>
                </template> 
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-pagination :current-page="page" :page-size="limit" style="padding: 30px 0; text-align:center;" background layout="total, prev, pager, next, jumper" :total="total" @current-change="getList"></el-pagination>
    </div>
</template>

<script>
import course from '@/api/edu/course'

export default {
    // 写核心代码位置
    data() {
        // 定义变量和初始值
        return {
            list : null,        // 查询之后接口返回集合
            page : 1,           // 当前页
            limit : 10,        // 每页记录数
            total : 0,          // 总记录数
            courseQuery : {}    // 条件封装对象
        }
    },
    created(){
        // 在页面渲染之前执行，一般调用 methods 定义的方法
        // 调用
        this.getList()

    },
    methods : {
        // 讲师课程的方法
        getList(page = 1){
            this.page = page
            course.pageCourseCondition(this.page, this.limit, this.courseQuery)
                .then(response => {   // 请求成功
                    // respose 接口返回的数据
                    this.list = response.data.list
                    this.total = response.data.total
                })
                .catch(error => {     // 请求失败
                    console.log(error)
                })
        },
        // 清空条件查询的条件
        resetData(){
            this.courseQuery = {}
            this.getList()
        },
        // 删除课程方法
        removeDataById(id){
            this.$confirm('此操作将永久删除该课程, 是否继续?', '提示', {
                confirmButtonText : '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                course.deleteCourse(id).then(response => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getList()
                })            
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '删除失败'
                });          
            });
        }
    }
}
</script>